import time

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec  # 提供等待条件
from common._dirver import Driver


class BaseActivity():
    def __init__(self):
        super().__init__()
        self.driver = Driver()

    def ele_locator(self, locator):
        return WebDriverWait(self.driver, 10, 0.5).until(ec.visibility_of_element_located(locator))

    def eles_locator(self, locator):
        return WebDriverWait(self.driver, 10, 0.5).until(ec.visibility_of_all_elements_located(locator))

    def ele_click(self, locator):
        self.ele_locator(locator).click()

    def ele_double_click(self, locator):
        pass

    def ele_sendkeys(self, locator, value, isclear=False):
        _ele_locator=self.ele_locator(locator)
        _ele_locator.click()
        # if isclear:
        #     _ele_locator.clear()
        _ele_locator.send_keys(value)
        #self.ele_locator(locator).send_keys(value)

    def ele_clear(self, locator):
        self.ele_locator(locator).clear()

    def ele_long_press(self, locator):
        pass

    def ele_keycode(self, keycode):
        self.driver.press_keycode(keycode)

    def _screenshot(self, filename):
        self.driver.save_screenshot(filename)

    def scroll_up(self):
        size = self.driver.get_window_size()
        width,height = size.get('width'),size.get('height')
        # 居中上滑
        start_x_end, start_y,end_y= width * 0.5,height * 0.9,height * 0.2
        self.driver.swipe(start_x_end, start_y, start_x_end, end_y, 1000)
        time.sleep(2)
